package com.item.service.impl;

import com.github.pagehelper.PageHelper;
import com.item.domain.EduMenuList;
import com.item.mapper.EduMenuListMapper;
import com.item.po.UserRoleRPo;
import com.item.service.EduMenuListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.ArrayUtils;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class EduMenuListServiceImpl implements EduMenuListService {

    @Autowired
    EduMenuListMapper eduMenuListMapper;


    public List<EduMenuList> findAll(int limit, int page, UserRoleRPo eduMenuList) {
        if (limit > 0 && page > 0) {
            PageHelper.startPage(limit, page);
        }
        return eduMenuListMapper.findAll(eduMenuList);
    }


    public int add(EduMenuList eduMenuList) {
        return eduMenuListMapper.insertSelective(eduMenuList);
    }


    public int update(EduMenuList eduMenuList) {
        return eduMenuListMapper.updateByPrimaryKey(eduMenuList);
    }

    @Override
    public int delete(int id) {
        return eduMenuListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<EduMenuList> eduMenuListAll() {
        return eduMenuListMapper.eduMenuListAll();
    }

    @Override
    public List<EduMenuList> eduMenuListAllRole(UserRoleRPo eduMenuList) {

        List<Integer> ids = eduMenuList.getIds();
        String[] array = new String[ids.size()];
        for (int i = 0; i < ids.size(); i++) {
            array[i] = ids.get(i).toString();
        }
        List<EduMenuList> eduMenuListssss = new ArrayList<>();
        List<EduMenuList> eduMenuLists = eduMenuListMapper.eduMenuListAllRole(eduMenuList);
        for (EduMenuList menuList : eduMenuLists) {
            List<EduMenuList> childrens = new ArrayList<>();
            List<EduMenuList> chiluren = eduMenuListMapper.eduMenuListChildren(menuList.getmId());
            for (EduMenuList eduMenuList1 : chiluren) {
                if (ArrayUtils.contains(array, eduMenuList1.getmId().toString())) {
                    EduMenuList eduMenuList11 = eduMenuList1;
                    eduMenuList11.setChildren(new ArrayList<>());
                    childrens.add(eduMenuList11);
                }
            }
            menuList.setChildren(childrens);
            //childrens.clear();
            eduMenuListssss.add(menuList);
        }
        return eduMenuLists;
    }
}
